<?php
include_once('lib/common.php');


class vmw_Login extends vmw_Page
{
    protected $pageError;


    public function __construct($title = 'Login')
    {
        parent::__construct($title);

        $this->backButton = false;
        $this->pageError = '';
    }


    public function SetError($error)
    {
        $this->pageError = $error;
    }


    protected function GetJQBody()
    {
        global $_COOKIE;

        if (isset($_COOKIE['vmw_Host']))
            $host = $_COOKIE['vmw_Host'];
        else
            $host = '';

        $content = parent::GetJQBody();

        if ($this->pageError != '')
        {
            $content .= '<div class="ui-body ui-body-e" style="text-align: center;">' . "\r\n";
            $content .= '  <p>' . htmlspecialchars($this->pageError) . '</p>' . "\r\n";
            $content .= '</div>' . "\r\n";
        }

        $content .= '<form data-ajax="false" action="' . $_SERVER['PHP_SELF'] . '" method="post">' . "\r\n";
        $content .= '  <input type="hidden" name="vmw_Page" value="Login" />' . "\r\n";
        $content .= '  <div class="ui-body">' . "\r\n";
        $content .= '    <div data-role="fieldcontain">' . "\r\n";
        $content .= '      <label for="server">Server:</label>' . "\r\n";
        $content .= '      <input type="text" name="server" id="server" value="' . $host . '" />' . "\r\n";
        $content .= '    </div>' . "\r\n";
        $content .= '    <div data-role="fieldcontain">' . "\r\n";
        $content .= '      <label for="username">Username:</label>' . "\r\n";
        $content .= '      <input type="text" name="username" id="username" value="" />' . "\r\n";
        $content .= '    </div>' . "\r\n";
        $content .= '    <div data-role="fieldcontain">' . "\r\n";
        $content .= '      <label for="server">Password:</label>' . "\r\n";
        $content .= '      <input type="password" name="password" id="password" value="" />' . "\r\n";
        $content .= '    </div>' . "\r\n";
        $content .= '    <button type="submit" data-theme="b">Login</button>' . "\r\n";
        $content .= '  </div>' . "\r\n";
        $content .= '</form>';

        return $content;
    }


    public function ProcessRequest()
    {
        global $_REQUEST;

        if (isset($_REQUEST['username']))
        {
            $svc = null;

            //
            // Perform an initial connection to the server.
            //
            try
            {
                $svc = new visvc('https://' . $_REQUEST['server'] . '/sdk/vimService.wsdl');
            }
            catch (Exception $e)
            {
                $this->SetError('Could not contact VMWare service.');
                echo $this->GetHTML();
            }

            //
            // Try to login with the given username and password.
            //
            try
            {
                $svc->sessionManager->Login($_REQUEST['username'], $_REQUEST['password']);
            }
            catch (Exception $e)
            {
                $this->SetError($e->getMessage());
                echo $this->GetHTML();
            }

            setcookie('vmw_Host', $_REQUEST['server']);
            setcookie('vmw_Session', $svc->GetSessionID());

            header('Location: home.php');
        }
        else
            echo $this->GetHTML();
    }
}

$page = new vmw_Login();
$page->ProcessRequest();
?>
